

// Sets up callbacks for state change
// You should not set up the state object until you get your first callback.
function init() {
   console.log("init");
   gapi.hangout.data.addStateChangeListener(noiseUpdated);

   gapi.hangout.addParticipantsListener(participantsUpdated);

}



function addVol(){
	console.log("vol is" +vol);
	var id = gapi.hangout.getParticipantId();
	var vol = gapi.hangout.av.getParticipantVolume(id);
	
	gapi.hangout.data.submitDelta({'vol': vol});

}

function volUpdated(delta, metadata){
	if(gapi.hangout.data.getState()['vol']) {	     
		var vol = gapi.hangout.data.getState()['vol'];
		
		console.log("vol Updated" + vol);
		
		changeEffect(vol);
		
	}else{
		console.log("vol undefined");
	}
	
}

function changeEffect(vol){
	speed = vol/10.0;
	console.log("volume is ", vol);

}


function noiseUpdated(delta, metadata){
	if(gapi.hangout.data.getState()['noise']) {	     
		var src = gapi.hangout.data.getState()['noise'];
		
		console.log("noise Updated" + src);
		
		playNoise(src);
		resetNoise();
		
	}else{
		console.log("noise undefined");
	}
}

	function playNoise(src){
		var audio = $('<audio />', { autoPlay : 'autoplay' });        
		 addSource(audio, src);    
		 
		 $("#play_area").html(audio);
		 
		
	
	}
	
	 function addSource(elem, path) {      
	 	$('<source />').attr('src', path).appendTo(elem);   
	 };   
	




function addNoise(src){
  	console.log("Button clicked addNoise");
   	console.log( gapi.hangout.data.getState()['noise'] );


	var id = gapi.hangout.getParticipantId();
	var name = gapi.hangout.getParticipantById(id).displayName;


	gapi.hangout.data.submitDelta({'noise': src});
}



// Note that the hangouts object is not set up until the gadget loads
gadgets.util.registerOnLoadHandler(init);
       
// Reset value of "count" to 0
function resetNoise(){      
	console.log("reset noise");
	gapi.hangout.data.submitDelta({'noise': ''});
}
